package history.link.doublylinklist;

import java.util.Iterator;

/**
 * @author czt
 * @version 1.0
 * @since 2024/10/24
 *
 * 单向链表-带哨兵，不管有没有节点，哨兵节点始终存在
 */
public class DoublyLinkedListSentinel implements Iterable<Integer>{

    private Node head; //头部哨兵

    private Node tail; //尾部哨兵

    public DoublyLinkedListSentinel() {
        head = new Node(null, 666,null);
        tail = new Node(null, 888,null);
        head.next = tail;
        tail.prev = head;
    }


    /**
     * 节点类
     */
    private static class Node{

        Node prev; //上一个节点指针

        int value; //值

        Node next; //下一个节点指针

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    //向链表头部添加数据
    public void addFirst(int value) {
        insert(0, value);
    }

    //遍历链表
    public void loop() {
        Node p = head.next;
        while (p != tail) {
            System.out.println("p = " + p.value);
            p = p.next;
        }
    }

    //遍历链表
    public void loop2() {
        for (Node p = head.next; p != tail; p = p.next) {
            System.out.println("p = " + p.value);
        }
    }

    //遍历
    @Override
    public Iterator<Integer> iterator() {
        return null;
    }

    //查找链表最后一个节点
    private Node findLast() {

        return null;
    }

    //向链表尾部添加节点
    public void addLast(int value) {
        Node prev = tail.prev;
        Node node = new Node(prev, value, tail);
        prev.next = node;
        tail.prev = node;
    }

    //根据索引获取节点
    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != tail; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    //返回要查找的节点值
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException();
        }
        return node.value;
    }

    //任意位置插入节点
    public void insert(int index, int value) {
        Node prev = findNode(index - 1); //要插入索引的上一个节点
        if (prev == null) {
            throw new IllegalArgumentException();
        }
        Node next = prev.next; //要插入索引的节点位置
        //待插入的节点
        Node inserted = new Node(prev, value, next);
        prev.next = inserted;
        next.prev = inserted;

    }

    //删除头节点
    public void removeFirst() {
        remove(0);
    }

    //按索引删除
    public void remove(int index) {
        Node prev = findNode(index - 1); //待删除节点的上一个节点
        if (prev == null) {
            throw new IllegalArgumentException();
        }

        Node removed = prev.next; //待删除节点
        if (removed == tail) {
            throw new IllegalArgumentException();
        }

        Node next = removed.next; //待删除节点的下一个节点

        prev.next = next;
        next.prev = prev;
    }

    public void removeLast() {
        //通过尾哨兵查找最后一个节点，即待删除的节点
        Node removed = tail.prev;
        //待删除节点的上一个节点
        Node prev = removed.prev;
        if (prev == head) {
            //如果待删除节点是头哨兵，则不能删除
            throw new IllegalArgumentException();
        }

        prev.next = tail;
        tail.prev = prev;
    }
}


